package com.msstudio.utils;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 

import com.msstudio.midlets.MSCMIDlet;
import java.util.Vector;

public class FriendRecord
    implements HttpPosterListener
{

    public static final int C_Group_None = 0;
    public static final int C_Group_Friend = 1;
    public static final int C_Group_Stranger = 2;
    public static final int C_MaxFriendNum = 300;
    public int iFriendNum;//��¼���к�����
    public int iStrangerNum;//��¼����İ������
    public int iTotleFriendNum;//�ܹ�������
    private boolean iInitFinished;
    private Vector vFriendList;//�����б?
    private Vector vStrangerList;//İ�����б?
    private int startIndex;
    private boolean timerOn;

    public FriendRecord()
    {//��ʼ��������
        iFriendNum = 0;
        iStrangerNum = 0;
        iTotleFriendNum = 0;
        iInitFinished = false;
        vFriendList = new Vector();
        vStrangerList = new Vector();
        startIndex = 0;
        timerOn = false;
    }

    public boolean addRecord(String s)
    {//�
        if(iFriendNum >= 300)
            return false;
        long l  = 0;
        try { 
            l = Long.parseLong(s);
        }
        catch(NumberFormatException numberformatexception)
        {
            return false;
        }
        if(l >= 0xc0a4680L && l <= 0xc1988bfL)
        {//��������202000000��202999999֮���򷵻ؼٺ��˳���
            return false;
        } else
        {//���һ�����Ѽ�¼��
            FriendRecordItem friendrecorditem = new FriendRecordItem(s);
            vFriendList.addElement(friendrecorditem);
            iFriendNum++;
            iInitFinished = false;
            return true;
        }
    }

    public void deleteRecord(String s)
    {//��ݺ���ɾ��һ����¼��
        int i = getUserLocation(s);//��ݺ���ȷ��Ҫɾ��ĺ������б��е�λ�ã�
        if(i >= 0)
            if(i < 300)
            {
                vFriendList.removeElementAt(i);
                iFriendNum--;
            } else
            {
                vStrangerList.removeElementAt(i - 300);
                iStrangerNum--;
            }
    }

    public final int getUserLocation(String s)
    {//��ݺ���ȷ��Ҫɾ��ĺ������б��е�λ�ã�
        int i;
        try
        {
            i = getUserLocation(Long.parseLong(s));
        }
        catch(NumberFormatException numberformatexception)
        {
            i = -1;
        }
        return i;
    }

    public void addStrangerRecord(String s)
    {//����һ��İ���˼�¼��
        FriendRecordItem friendrecorditem = new FriendRecordItem(s);
        friendrecorditem.sState = 10;
        vStrangerList.addElement(friendrecorditem);
        MSCMIDlet.finger.doGetInfor(s, null);
        iStrangerNum++;
    }

    public int buildRecord(String as[])
    {
        int i = as.length;
        if(i > 300)
            return -1;
        int j;
        for(j = 0; j < i; j++)
            addRecord(as[j]);

        return j;
    }

    public int updateOnlineRecord(String as[], String as1[], String as2[], String as3[])
    {
        int i = as.length;
        if(i > 300 || i != as1.length || i != as2.length || i != as3.length)
            return -1;
        for(int k = 0; k < vFriendList.size(); k++)
            if(MSCMIDlet.getMsgDlg.findMsgByUIN(((FriendRecordItem)vFriendList.elementAt(k)).lUserID) < 0 && ((FriendRecordItem)vFriendList.elementAt(k)).sState > 0)
                ((FriendRecordItem)vFriendList.elementAt(k)).sState = 1;

        for(int l = 0; l < as.length; l++)
        {
            int j = getUserLocation(as[l]);
            if(j < 0)
            {
                if(!addRecord(as[l]))
                    continue;
                j = vFriendList.size() - 1;
            }
            if(j < vFriendList.size())
            {
                ((FriendRecordItem)vFriendList.elementAt(j)).strName = PkgTools.Decodecgi(as1[l]);
                try
                {
                    ((FriendRecordItem)vFriendList.elementAt(j)).sState = Short.parseShort(as3[l]);
                    ((FriendRecordItem)vFriendList.elementAt(j)).sFaceID = Short.parseShort(as2[l]);
                }
                catch(NumberFormatException numberformatexception) { }
            }
        }

        MSCMIDlet.friendList.buildFriendList();
        return i;
    }

    public void updateStat(String s, String s1)
    {
        int i;
        i = getUserLocation(s);
        if(i < 0)
            return;
        try
        {
            if(i < 300)
                ((FriendRecordItem)vFriendList.elementAt(i)).sState = Short.parseShort(s1);
            else
                ((FriendRecordItem)vStrangerList.elementAt(i - 300)).sState = Short.parseShort(s1);
        }
        catch(Exception exception) { }
        return;
    }

    public final long getUserIDbyIndex(int i)
    {
        if(i < 0 || i >= vFriendList.size() && i < 300 || i >= vStrangerList.size() + 300)
            return 0L;
        if(i < 300)
            return ((FriendRecordItem)vFriendList.elementAt(i)).lUserID;
        else
            return ((FriendRecordItem)vStrangerList.elementAt(i - 300)).lUserID;
    }

    public final String getstrUserIDbyIndex(int i)
    {
        if(i < 0 || i >= vFriendList.size() && i < 300 || i >= vStrangerList.size() + 300)
            return "0";
        if(i < 300)
            return ((FriendRecordItem)vFriendList.elementAt(i)).strUserID;
        else
            return ((FriendRecordItem)vStrangerList.elementAt(i - 300)).strUserID;
    }

    public final String getNamebyIndex(int i)
    {
        if(i < 0 || i >= vFriendList.size() && i < 300 || i >= vStrangerList.size() + 300)
            return "";
        if(i < 300)
            return ((FriendRecordItem)vFriendList.elementAt(i)).strName;
        else
            return ((FriendRecordItem)vStrangerList.elementAt(i - 300)).strName;
    }

    public final short getStatebyIndex(int i)
    {
        if(i < 0 || i >= vFriendList.size() && i < 300 || i >= vStrangerList.size() + 300)
            return 0;
        if(i < 300)
            return ((FriendRecordItem)vFriendList.elementAt(i)).sState;
        else
            return ((FriendRecordItem)vStrangerList.elementAt(i - 300)).sState;
    }

    public final short getFaceIDbyIndex(int i)
    {
        if(i < 0 || i >= vFriendList.size() && i < 300 || i >= vStrangerList.size() + 300)
            return 0;
        if(i < 300)
            return ((FriendRecordItem)vFriendList.elementAt(i)).sFaceID;
        else
            return ((FriendRecordItem)vStrangerList.elementAt(i - 300)).sFaceID;
    }

    public final int getUserLocation(long l)
    {
        for(int i = 0; i < vFriendList.size(); i++)
            if(getUserIDbyIndex(i) == l)
                return i;

        for(int j = 0; j < vStrangerList.size(); j++)
            if(getUserIDbyIndex(j + 300) == l)
                return j + 300;

        return -1;
    }

    public final String getNamebyID(long l)
    {
        int i = getUserLocation(l);
        if(i >= 0)
            return getNamebyIndex(i);
        else
            return "" + l;
    }

    public final short getFaceIDbyUID(long l)
    {
        int i = getUserLocation(l);
        if(i >= 0)
            return getFaceIDbyIndex(i);
        else
            return -1;
    }

    public int getFriendNum()
    {
        return iFriendNum;
    }

    public int getStrangerNum()
    {
        return iStrangerNum;
    }

    private String GetNoStateID()
    {
        for(int i = startIndex; i < vFriendList.size(); i++)
            if(getStatebyIndex(i) == 0)
            {
                startIndex = i + 1;
                return getstrUserIDbyIndex(i);
            }

        for(int j = 0; j < startIndex; j++)
            if(getStatebyIndex(j) == 0)
            {
                startIndex = j + 1;
                return getstrUserIDbyIndex(j);
            }

        return null;
    }

    public void startTimer()
    {
        timerOn = true;
    }

    public void stopTimer()
    {
        timerOn = false;
    }

    public void OnTimer()
    {
        if(!timerOn)
            return;
        if(MSCMIDlet.getMsgDlg.getControl())
            return;
        if(!iInitFinished)
        {
            String s = GetNoStateID();
            if(s != null)
                MSCMIDlet.httpPoster.getInfor(s, this);
            iInitFinished = true;
        }
    }

    public void SetInfor(String s, boolean flag, String s1, String s2)
    {
        int i = getUserLocation(s);
        if(i < 0)
            return;
        try
        {
            SetInforByIndex(i, flag, s1, Short.parseShort(s2));
        }
        catch(Exception exception) { }
    }

    public void SetInforByIndex(int i, boolean flag, String s, short word0)
    {
        if(i < 300)
        {
            if(((FriendRecordItem)vFriendList.elementAt(i)).sState == 0)
                ((FriendRecordItem)vFriendList.elementAt(i)).sState = 1;
            else
            if(flag)
                return;
            ((FriendRecordItem)vFriendList.elementAt(i)).strName = s;
            ((FriendRecordItem)vFriendList.elementAt(i)).sFaceID = word0;
        } else
        {
            i -= 300;
            ((FriendRecordItem)vStrangerList.elementAt(i)).sState = 1;
            ((FriendRecordItem)vStrangerList.elementAt(i)).strName = s;
            ((FriendRecordItem)vStrangerList.elementAt(i)).sFaceID = word0;
        }
    }

    public void loadFriendInfor()
    {
        PkgTools.loadFriendData();
        MSCMIDlet.friendList.buildFriendList();
    }

    public void saveFriendInfor()
    {
        PkgTools.saveFriendData(vFriendList);
    }

    public void receiveHttpResponse(String s)
    {
        String s1 = PkgTools.GetParaVal("CMD", s).toUpperCase();
        String s2 = PkgTools.GetParaVal("RES", s);
        if(s1.equals("GETINFO") && s2.equals("0"))
            SetInfor(PkgTools.GetParaVal("UN", s), true, PkgTools.Decodecgi(PkgTools.GetParaVal("NK", s)), PkgTools.GetParaVal("FC", s));
        iInitFinished = false;
    }

    public void handleHttpError(String s)
    {
        iInitFinished = false;
    }

    public int OnlineAndAway(int i)
    {
        short word0 = getStatebyIndex(i);
        return word0 != 10 && word0 != 30 ? 0 : 1;
    }

    public int GetActiveData(int ai[], int i, int j)
    {
        int k1 = getStrangerNum();
        if(iTotleFriendNum + k1 + iFriendNum == 0)
            return 0;
        int j1 = 0;
        switch(i)
        {
        default:
            break;

        case 1: // '\001'
            for(int k = 0; k < iFriendNum; k++)
                if(OnlineAndAway(k) != 0 && !MSCMIDlet.blackList.inBlackList(getUserIDbyIndex(k)))
                    ai[j1++] = k;

            if(j != 0)
            {
                for(int l = 0; l < iFriendNum; l++)
                    if(OnlineAndAway(l) == 0 && !MSCMIDlet.blackList.inBlackList(getUserIDbyIndex(l)))
                        ai[j1++] = l;

            }
            break;

        case 2: // '\002'
            for(int i1 = 0; i1 < iStrangerNum; i1++)
                if(!MSCMIDlet.blackList.inBlackList(getUserIDbyIndex(i1 + 300)))
                    ai[j1++] = i1 + 300;

            break;
        }
        return j1;
    }

    public int getGroupByIndex(int i)
    {
        return i >= 300 ? 2 : 1;
    }
}
